home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / CLTHREAD.CPP < prev    next >
C/C++ Source or Header  |  1993-09-02  |  10KB  |  316 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    class.cpp
  5. //   Title:    C++ Class Libraries
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains code for the class CL_THREAD.
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <class.hpp>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //   Description:    Default constructor
  45. //    Parameters:
  46. //       Returns:    
  47. //----------------------------------------------------------------------------
  48. FN_M CL_THREAD::CL_THREAD()
  49. {
  50.     CL_THREAD::Initialize(CL_INIT_CLASS);
  51. }
  52.  
  53.  
  54. //----------------------------------------------------------------------------
  55. //   Description:    Copy constructor
  56. //    Parameters:    rccl_thread        Reference to object to copy.
  57. //       Returns:    
  58. //----------------------------------------------------------------------------
  59. FN_M CL_THREAD::CL_THREAD(RCCL_THREAD rccl_thread)
  60. {
  61.     CL_THREAD::Initialize(CL_INIT_CLASS);
  62.     *this = rccl_thread;
  63. }
  64.  
  65.  
  66. //----------------------------------------------------------------------------
  67. //   Description:    Destructor
  68. //    Parameters:
  69. //       Returns:    
  70. //----------------------------------------------------------------------------
  71. FN_M CL_THREAD::~CL_THREAD()
  72. {
  73.     CL_THREAD::Destroy(FALSE);
  74. }
  75.  
  76.  
  77. //----------------------------------------------------------------------------
  78. //   Description:    Destroy object. Free any resources used by object.
  79. //                          Normally called by destructor.
  80. //                        Should allow multiple calls from various classes.
  81. //    Parameters:    fDestroyAll        Destroy parents also?
  82. //                                            Default is TRUE.
  83. //       Returns:    TRUE if successful.
  84. //----------------------------------------------------------------------------
  85. BOOL FN_M CL_THREAD::Destroy(BOOL fDestroyAll)
  86. {
  87.     CL_THREAD::Initialize(CL_INIT_CLASS_VARS);
  88.     if (fDestroyAll)                            // Destroy parent.
  89.         CL_THREAD_PARENT::Destroy(fDestroyAll);                    
  90.     return TRUE;
  91. }
  92.  
  93.  
  94. //----------------------------------------------------------------------------
  95. //   Description:    Initialize object. 
  96. //                          Normally called by constructor.
  97. //                        Should allow multiple calls from various classes.
  98. //    Parameters:    sInit        Initialization code. May be one of the following:
  99. //                                        CL_INIT_CLASS            Reset class variables and
  100. //                                                                    and dynamic allocations for
  101. //                                                                    this class only.
  102. //                                        CL_INIT_CLASS_VARS    Reset class variables for
  103. //                                                                    this class only.
  104. //                                        CL_INIT_VARS            Reset class variables for
  105. //                                                                    this class only.
  106. //                                        CL_INIT_ALL                Initialize class and all 
  107. //                                                                    parent class, including
  108. //                                                                    dynamic memory allocation.
  109. //                                    Default is CL_INIT_ALL
  110. //       Returns:    TRUE if successful.
  111. //----------------------------------------------------------------------------
  112. BOOL FN_M CL_THREAD::Initialize(SHORT sInit)
  113. {
  114.     if (sInit == CL_INIT_VARS || sInit == CL_INIT_ALL)
  115.         CL_THREAD_PARENT::Initialize(sInit);
  116.  
  117.     tid = 0;
  118.     fActive = FALSE;
  119.     if (event.IsError())
  120.         SetError(event.ErrorCode());
  121.  
  122.     return TRUE;
  123. }
  124.  
  125.  
  126. //----------------------------------------------------------------------------
  127. //   Description:    Check if object is in error state.
  128. //                          IsValid() && IsError() MUST NOT BE DEPENDENT ON ONE ANOTHER.
  129. //    Parameters:
  130. //       Returns:    TRUE if in error state.
  131. //----------------------------------------------------------------------------
  132. BOOL FN_M CL_THREAD::IsError() const
  133. {
  134.     return CL_THREAD_PARENT::IsError();
  135. }
  136.  
  137.  
  138. //----------------------------------------------------------------------------
  139. //   Description:    Check if object is valid
  140. //                          IsValid() && IsError() MUST NOT BE DEPENDENT ON ONE ANOTHER.
  141. //    Parameters:
  142. //       Returns:    TRUE if valid
  143. //----------------------------------------------------------------------------
  144. BOOL FN_M CL_THREAD::IsValid() const
  145. {
  146.     return CL_THREAD_PARENT::IsValid();
  147. }
  148.  
  149.  
  150. //----------------------------------------------------------------------------
  151. //   Description:    Assignment operator
  152. //                          NOTE: Don't copy object into self
  153. //    Parameters:    rccl_thread        Reference to right value.
  154. //       Returns:    Reference to new object.
  155. //----------------------------------------------------------------------------
  156. RCCL_THREAD FN_M CL_THREAD::operator=(RCCL_THREAD rccl_thread)
  157. {
  158.     if (this != &rccl_thread)
  159.         {
  160.         Invalid("CL_THREAD::operator=");
  161.         }
  162.     return (RCCL_THREAD)*this;
  163. }
  164.  
  165.  
  166. //----------------------------------------------------------------------------
  167. //   Description:    Retrieve object from persistent storage
  168. //    Parameters:    pcsz        Name of object.
  169. //                        pcszSub    Sub-name of object.
  170. //                                    The first character of the name should be '~'.
  171. //                                    If NULL, no sub name is available.
  172. //                                    Default is NULL
  173. //       Returns:    TRUE if successful.
  174. //----------------------------------------------------------------------------
  175. BOOL FN_M CL_THREAD::Retrieve(PCSZ pcsz, PCSZ pcszSub)
  176. {
  177.     NOTUSED(pcsz);
  178.     NOTUSED(pcszSub);
  179.     Invalid("CL_THREAD::Retrieve");
  180.     return FALSE;
  181. }
  182.  
  183.  
  184. //----------------------------------------------------------------------------
  185. //   Description:    Start a thread object.
  186. //    Parameters:    
  187. //       Returns:    TRUE if successful.
  188. //----------------------------------------------------------------------------
  189. BOOL FN_M CL_THREAD::Run()
  190. {
  191.     if (IsError())
  192.         return FALSE;
  193.  
  194.     if (!ThreadStart(ThreadFunc, &tid, (PVOID)this))
  195.         return FALSE;
  196.  
  197.     if (tid <= 0)
  198.         return FALSE;
  199.  
  200.     fActive = TRUE;
  201.     Sleep(0);                                    // Let thread start
  202.     return TRUE;
  203. }
  204.  
  205.  
  206. //----------------------------------------------------------------------------
  207. //   Description:    Store object to persistent storage
  208. //    Parameters:    pcsz        Name of object.
  209. //                        pcszSub    Sub-name of object.
  210. //                                    The first character of the name should be '~'.
  211. //                                    If NULL, no sub name is available.
  212. //                                    Default is NULL
  213. //       Returns:    TRUE if successful.
  214. //----------------------------------------------------------------------------
  215. BOOL FN_M CL_THREAD::Store(PCSZ pcsz, PCSZ pcszSub)
  216. {
  217.     NOTUSED(pcsz);
  218.     NOTUSED(pcszSub);
  219.     Invalid("CL_THREAD::Store");
  220.     return FALSE;
  221. }
  222.  
  223.  
  224. //----------------------------------------------------------------------------
  225. //   Description:    Thread function.
  226. //    Parameters:
  227. //       Returns:    TRUE if successful.
  228. //----------------------------------------------------------------------------
  229. VOID FN_T CL_THREAD::ThreadFunc(PVOID pv)
  230. {
  231.     PCL_THREAD pcl_thread = (PCL_THREAD)pv;
  232.  
  233.     pcl_thread->Thread();                    // Call thread function
  234.  
  235.     pcl_thread->tid = 0;                        // Clear variables
  236.     pcl_thread->fActive = FALSE;
  237.     pcl_thread->event.Post();
  238.     return ;
  239. }
  240.  
  241.  
  242. //----------------------------------------------------------------------------
  243. //   Description:    Wait for thread to terminate.
  244. //    Parameters:
  245. //       Returns:    TRUE if successful.
  246. //----------------------------------------------------------------------------
  247. BOOL FN_M CL_THREAD::Wait(ULONG ulWait)
  248. {
  249.     return event.Wait(ulWait);
  250. }
  251.  
  252.  
  253. //----------------------------------------------------------------------------
  254. //   Description:    Print object value to debugging output.
  255. //    Parameters:    pccl_thread        Pointer to dynamic object. 
  256. //                                    If NULL, static data elements are printed.
  257. //                                    Default is NULL.
  258. //                        pcsz        Name of object.
  259. //                                    If NULL, no name is displayed.
  260. //                                    Default is NULL.
  261. //                        cLevel    Display level. 
  262. //                                    Default is zero.
  263. //       Returns:
  264. //----------------------------------------------------------------------------
  265. #if COMPILE_DEBUG
  266. VOID FN_M CL_THREAD::Print(PCCL_THREAD pccl_thread, PCSZ pcsz, SIZET cLevel)
  267. {
  268. #if COMPILE_TEST
  269.     OutputL(cLevel, "CL_THREAD%s%s", (pcsz?"::":""), (pcsz?pcsz:""));
  270.     cLevel++;
  271.     if (pccl_thread)
  272.         {
  273.         Output(" <%p>\n", pccl_thread);
  274.         if(!pccl_thread->IsError())
  275.             {
  276. //            OutputL(cLevel, " = %d\n", pccl_thread->);
  277.             }
  278.         }
  279.     else
  280.         Output(" <NULL>\n");
  281.     CL_THREAD_PARENT::Print((CL_THREAD_PARENT _FAR_ *)pccl_thread, pcsz, cLevel);
  282.     return ;
  283. #else
  284.     NOTUSED(cLevel);
  285.     NOTUSED(pccl_thread);
  286.     NOTUSED(pcsz);
  287.     return ;
  288. #endif
  289. }
  290. #endif
  291.  
  292.  
  293. //----------------------------------------------------------------------------
  294. //   Description:    Run standard test suite on object.
  295. //    Parameters:    sTest        Test to run.
  296. //                                    If 0, run default tests.
  297. //                                    Default is 0.
  298. //       Returns:    TRUE if successful.
  299. //----------------------------------------------------------------------------
  300. #if COMPILE_DEBUG
  301. BOOL FN_M CL_THREAD::Test(SHORT sTest)
  302. {
  303. #if COMPILE_TEST
  304.     NOTUSED(sTest);
  305.     return TRUE;
  306. #else
  307.     NOTUSED(sTest);
  308.     return TRUE;
  309. #endif
  310. }
  311. #endif
  312. //----------------------------------------------------------------------------
  313. //------------------------------- End of File --------------------------------
  314. //----------------------------------------------------------------------------
  315.  
  316.